package userInterface;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class Lists
	extends MIDlet
	implements CommandListener
{
	private Display mDisplay;
	private List mMainList, mUsersList, mComputersList;
	private Command mOKCommand;
	
	protected void startApp() throws MIDletStateChangeException {
		if (mMainList == null) {
			// universal OK button
			mOKCommand = new Command("OK", Command.OK, 0);
			// the main list
			mMainList = new List("Select configuration!", List.IMPLICIT);
			Image computerImage = null;
			Image usersImage = null;
			Image exitImage = null;
			try {
				computerImage = Image.createImage("/computer.png");
				usersImage = Image.createImage("/users.png");
				exitImage = Image.createImage("/exit.png");
			} 
			catch (java.io.IOException ioe) {} 
			catch (Exception e) {}
			mMainList.append("Users", usersImage);
			mMainList.append("Computers", computerImage);
			mMainList.append("Exit", exitImage);
			mMainList.setCommandListener(this);
			// the users list
			mUsersList = new List("Select an user!", List.EXCLUSIVE);
			mUsersList.append("Petru", null);
			mUsersList.append("Ion", null);
			mUsersList.append("Vasile", null);
			mUsersList.append("Ghita", null);
			mUsersList.addCommand(mOKCommand);
			mUsersList.setCommandListener(this);
			// the computers list
			mComputersList = new List("Select some computers!", List.MULTIPLE);
			mComputersList.append("MAC laptop", null);
			mComputersList.append("Windows desktop", null);
			mComputersList.append("Linux netbook", null);
			mComputersList.addCommand(mOKCommand);
			mComputersList.setCommandListener(this);
			
			mDisplay = Display.getDisplay(this);
		}
		mDisplay.setCurrent(mMainList);
	}
	
	protected void pauseApp() {
		
	}

	protected void destroyApp(boolean arg0) {
		
	}

	public void commandAction(Command c, Displayable d) {
		if (c == List.SELECT_COMMAND && d == mMainList) {
			int actionIndex = mMainList.getSelectedIndex();
			if (actionIndex < 0)
				return;
			String actionStr = mMainList.getString(actionIndex);
			if(actionStr.equals("Exit")) {
				destroyApp(true);
				notifyDestroyed();
			} else if (actionStr.equals("Users")) {
				mDisplay.setCurrent(mUsersList);
			} else if (actionStr.equals("Computers")) {
				mDisplay.setCurrent(mComputersList);
			}
		} else if (c == mOKCommand) {
			mDisplay.setCurrent(mMainList);
		}
	}
	

}
